package com.example.sunmiv1.room

import android.util.Log
import com.example.sunmiv1.room.entity.MerchandisePrintEntity
import com.example.sunmiv1.utils.GsonTools
import com.example.sunmiv1.utils.net.ACache
import com.google.gson.Gson
import io.realm.Realm
import java.util.UUID


object MerchandiseDaoManager {
    private val TAG=javaClass.simpleName
    suspend fun add(entity: MerchandisePrintEntity){
        Log.e(TAG, "add: entity=${Gson().toJson(entity)}", )
        // 获的realm
        val realm = Realm.getDefaultInstance();
        realm.beginTransaction()
        val merchantid=ACache.get(BaseApp.getInstall()).getAsString("merchantid")?:""
        val result = realm.where(MerchandisePrintEntity::class.java)
            .equalTo("userId",merchantid)
            .and()
            .equalTo("merchandiseId",entity.merchandiseId)
            .findAll()
        val json=result.asJSON()
        Log.e(TAG, "add: json=$json", )
        val list=GsonTools.getObjectList(json,MerchandisePrintEntity::class.java)
        Log.e(TAG, "add: size=${list.size} list=${GsonTools.beanToJson(list)}", )
        if(result.size>0){
            entity.userId=merchantid
            result.setString("userId",entity.userId)
            result.setString("merchandiseId",entity.merchandiseId)
            result.setString("merchandiseName",entity.merchandiseName)
            result.setBoolean("isPrint",entity.isPrint)
        }else{
            //在这里里面插入或者更新
            val user = realm.createObject(MerchandisePrintEntity::class.java,System.currentTimeMillis());//插入数据
            entity.userId=merchantid
            user.copy(entity)
        }
        realm.commitTransaction();
        realm.close()
    }
    suspend fun getAll():List<MerchandisePrintEntity>{
        // 获的realm
        val realm = Realm.getDefaultInstance();

        realm.beginTransaction();
        val merchantid=ACache.get(BaseApp.getInstall()).getAsString("merchantid")?:""
        val result = realm.where(MerchandisePrintEntity::class.java)
            .equalTo("userId",merchantid)
            .findAll();
        Log.e(TAG, "getAll: size=${result.size}", )
        val json=result.asJSON()
        Log.e(TAG, "getAll: json=$json", )
        val list=GsonTools.getObjectList(json,MerchandisePrintEntity::class.java)
        Log.e(TAG, "getAll: size=${list.size} list=${GsonTools.beanToJson(list)}", )
        realm.commitTransaction();
        realm.close()
        return list
    }
    suspend fun findMerchandiseId(id:String):List<MerchandisePrintEntity>{
        Log.e(TAG, "findMerchandiseId: id=$id", )
        // 获的realm
        val realm = Realm.getDefaultInstance();
        realm.beginTransaction();
        val merchantid=ACache.get(BaseApp.getInstall()).getAsString("merchantid")?:""
        val result = realm.where(MerchandisePrintEntity::class.java)
            .equalTo("userId",merchantid)
            .and()
            .equalTo("merchandiseId",id)
            .findAll();
        Log.e(TAG, "findMerchandiseId: size=${result.size}", )
        val json=result.asJSON()
        Log.e(TAG, "findMerchandiseId: json=$json", )
        val list=GsonTools.getObjectList(json,MerchandisePrintEntity::class.java)
        Log.e(TAG, "findMerchandiseId: size=${list.size} list=${GsonTools.beanToJson(list)}", )
        realm.commitTransaction();
        realm.close()
        return list
    }
}